package com.sense.sensedialprehandle.utils;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class OkHttpUtil {
    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");//mdiatype 这个需要和服务端保持一致
    private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");//mdiatype 这个需要和服务端保持一致
    private OkHttpUtil() {}

    private static class OkHttpUtilInstance{
        private static final OkHttpUtil instance=new OkHttpUtil();
    }

    public static OkHttpUtil getInstance(){
        return OkHttpUtilInstance.instance;
    }

    /**
     * 同步get请求
     * @param url
     * @return
     * @throws IOException
     */

    public String doGet(String url) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    /**
     * 同步post 请求
     * @param url
     * @param paramsMap
     * @return
     * @throws IOException
     */
    @Deprecated
    public String doPost(String url, Map<String,Object> paramsMap) throws IOException {
        //处理参数
        StringBuilder tempParams = new StringBuilder();
        int pos = 0;
        for (String key : paramsMap.keySet()) {
            if (pos > 0) {
                tempParams.append("&");
            }
            tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key).toString(), "utf-8")));
            pos++;
        }
        //生成参数
        String params = tempParams.toString();
        //创建一个请求实体对象 RequestBody
        RequestBody requestBody = RequestBody.create(params,MEDIA_TYPE_JSON);
        Request request=new Request.Builder().url(url).post(requestBody).build();
        OkHttpClient client=new OkHttpClient();
        Response execute = client.newCall(request).execute();
        return execute.body().string();
    }

    /**
     * 同步post 请求
     * @param url
     * @return
     */
    public String post(String url, Map<String,Object> paramsMap) throws IOException {
        //处理参数
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        Iterator<String> it = paramsMap.keySet().iterator();
        while(it.hasNext()){
            String key=it.next();
            builder.add(key, (String) paramsMap.get(key));
        }
        RequestBody requestBody =builder.build();
        Request request=new Request.Builder().url(url).post(requestBody).build();
        Response execute = okHttpClient.newCall(request).execute();
        return execute.body().string();
    }

    public static void main(String[] args) {
        String url="http://101.132.86.237:18090/sense-admin/login_login";
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("KEYDATA","qq313596790fhzcx,fh,123456QQ978336446fh,fh,");
            String html=getInstance().post(url,map);
            System.out.println(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
